export const transferTypeEnum = {
  trim: "trim",
  trimEnd: "trimEnd",
  int: "int",
  float: "float",
  bool: "bool",
  intOrNull: "intOrNull",
  floatOrNull: "floatOrNull",
  intOrEptStr: "intOrEptStr",
  floatOrEptStr: "floatOrEptStr",
};



export const transfer = function (data, rules) {
  for (var i = 0; i < rules.length; i++) {
    switch (rules[i].type) {
      case "trim":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = data[rules[i].name].trim();
        }
        break;
      case "trimEnd":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = data[rules[i].name].trimEnd();
        }
        break;
      case "int":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseInt(data[rules[i].name]))
            ? 0
            : parseInt(data[rules[i].name]);
        }
        break;
      case "intOrNull":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseInt(data[rules[i].name]))
            ? null
            : parseInt(data[rules[i].name]);
        }
        break;
      case "intOrEptStr":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseInt(data[rules[i].name]))
            ? ""
            : parseInt(data[rules[i].name]);
        }
        break;
      case "float":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseFloat(data[rules[i].name]))
            ? 0
            : parseFloat(data[rules[i].name]);
        }
        break;
      case "floatOrNull":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseFloat(data[rules[i].name]))
            ? null
            : parseFloat(data[rules[i].name]);
        }
        break;
      case "floatOrEptStr":
        if (data[rules[i].name] != null && data[rules[i].name] != undefined) {
          data[rules[i].name] = isNaN(parseFloat(data[rules[i].name]))
            ? ""
            : parseFloat(data[rules[i].name]);
        }
        break;

      case "bool":
        data[rules[i].name] = data[rules[i].name] ? true : false;
        break;
      case "timestampToDate":
        var date = new Date(data[rules[i].name]);
        data[rules[i].name] = `${date.getFullYear()}-${
          date.getMonth() + 1
        }-${date.getDate()}`;
        break;
      case "timestampToDateTime":
        var date = new Date(data[rules[i].name]);
        data[rules[i].name] = `${date.getFullYear()}-${
          date.getMonth() + 1
        }-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
        break;
    }
  }
  return data;
};
